
citysize_rep_scatterplot <-
  analysis_df_3 %>%
  filter(occ %in% c('blue_collar', 'white_collar') & YEAR < all) %>%
  group_by(city) %>%
  filter(min(YEAR - all)==(YEAR-all)) %>%
  mutate(black_x_native_rep = govt_black_x_native_born - black_x_native_born,
         white_x_foreign_rep = govt_white_x_foreign_born - white_x_foreign_born,
         white_x_native_rep = govt_white_x_native_born - white_x_native_born) %>%
  select(n_total, black_x_native_rep, white_x_foreign_rep, white_x_native_rep, occ) %>%
  pivot_longer(cols = c(black_x_native_rep, white_x_foreign_rep, white_x_native_rep)) %>%
  mutate(name = case_when(name=='black_x_native_rep' ~ 'Black Native Born',
                          name=='white_x_native_rep' ~ 'White Native Born',
                          name=='white_x_foreign_rep' ~ 'White Foreign Born'),
         occ = case_when(occ=='blue_collar' ~ "Blue Collar",
                         occ=='white_collar' ~ "White Collar")) %>%
  ggplot(., aes(x = log(n_total), y = value)) +
  geom_point(alpha=.1) +
  #geom_smooth(method='lm', col='black') + 
  facet_grid(occ~name) + 
  theme_bw() + 
  labs(x = 'Log City Population', y = 'Representation in City Gov\'t\nRelative to Population') + 
  theme(legend.title=element_blank(), text=element_text(size=20)) +
  #theme(text=element_text(size=14, family="Times")) + 
  theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank()) + 
  theme(strip.background =element_rect(fill="white")) + 
  theme(legend.position='bottom') +
  theme(text=element_text(size=9))

city_size_white_scatterplot <-
  analysis_df_4 %>%
  filter(occ %in% c('blue_collar', 'white_collar') & YEAR < all) %>%
  group_by(city) %>%
  filter(min(YEAR - all)==(YEAR-all)) %>%
  mutate(irish_rep = govt_irish - irish,
         german_rep = govt_german - german,
         italian_rep = govt_italian - italian,
         polish_rep = govt_polish - polish,
         russian_rep = govt_russian - russian) %>%
  select(n_total, irish_rep, german_rep, italian_rep, polish_rep, russian_rep, occ) %>%
  pivot_longer(cols = c(irish_rep, german_rep, italian_rep, polish_rep, russian_rep)) %>%
  mutate(name = case_when(name=='irish_rep' ~ 'Irish',
                          name=='german_rep' ~ 'German',
                          name=='italian_rep' ~ 'Italian',
                          name=='polish_rep' ~ 'Polish',
                          name=='russian_rep' ~ 'Russian'),
         occ = case_when(occ=='blue_collar' ~ "Blue Collar",
                         occ=='white_collar' ~ "White Collar")) %>%
  ggplot(., aes(x = log(n_total), y = value)) +
  geom_point(alpha=.1) +
  facet_grid(occ~name) + 
  theme_bw() + 
  labs(x = 'Log City Population', y = 'Representation in City Gov\'t\nRelative to Population') + 
  ylim(-.3,.3) + 
  theme(legend.title=element_blank(), text=element_text(size=20)) +
  theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank()) + 
  theme(strip.background =element_rect(fill="white")) + 
  theme(legend.position='bottom') +
  theme(text=element_text(size=9))

#ggsave('./replication_file/_5_outputs/figures/citysize_rep_scatterplot.png', plot = citysize_rep_scatterplot, width=8, height=6)
#ggsave('./replication_file/_5_outputs/figures/citysize_rep_scatterplot2.png', plot = city_size_white_scatterplot, width=8, height=6)

ggsave('./replication_file/_5_outputs/figures/figure_a1_1.png', plot = citysize_rep_scatterplot, width=8, height=6)
ggsave('./replication_file/_5_outputs/figures/figure_a1_2.png', plot = city_size_white_scatterplot, width=8, height=6)


